package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.dto.ComplaintSolutionReportAnswer;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.ComplaintSolutionService;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import io.swagger.annotations.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.collections.map.LRUMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.security.Principal;

@Api(description = "投诉解决方案相关接口")
@RestController
@RequestMapping("/complaintSolutionReport")
public class ComplaintSolutionController {

    @Autowired
    private ComplaintSolutionService complaintSolutionService;

    @Autowired
    private UserRepository userRepository;
    // 给投诉报告处理上锁
    private LRUMap locks = new LRUMap(100);

    @ApiOperation("领取投诉报告")
    @RequestMapping(value = "claimComplaintReport", method = RequestMethod.GET)
    public JsonResponse claimComplaintReport(@ApiIgnore Principal principal,
        @ApiParam("投诉报告ID") @RequestParam(value = "complaintReportId") Long complaintReportId) {
        if (principal == null) {
            return JsonResponse.errorResponseWithError("抱歉", "invalid_token");
        }
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        return complaintSolutionService.claimComplaintReport(clientUser, complaintReportId);
    }

    @ApiOperation("提交投诉报解决方案答案")
    @RequestMapping(value = "submitComplaintSolutionReport", method = RequestMethod.POST)
    public JsonResponse submitComplaintSolutionReport(@ApiIgnore Principal principal,
        @ApiParam("投诉解决方案答案") @RequestBody ComplaintSolutionReportAnswer complaintSolutionReportList,
        @ApiParam("投诉报告ID") @RequestParam(value = "complaintReportId") Long complaintReportId,
        @ApiParam("投诉解决方案ID") @RequestParam(value = "complaintSolutionReportId", required = false) Long complaintSolutionReportId) {
        if (principal == null) {
            return JsonResponse.errorResponseWithError("抱歉", "invalid_token");
        }
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        Lock lock = (Lock) locks.get(complaintReportId);
        if (lock == null) {
            lock = (Lock) locks.put(complaintReportId, new ReentrantLock());
        }
        if (lock != null) {
            boolean tryLock = lock.tryLock();
            if (!tryLock) {
                return JsonResponse.errorResponseWithError("抱歉", "请勿重复操作!");
            }
        } else {
            return JsonResponse.errorResponseWithError("抱歉", "请勿重复操作!");
        }
        try {
            return complaintSolutionService.submitComplaintSolutionReport(clientUser, complaintSolutionReportList, complaintReportId, complaintSolutionReportId);

        } catch (Exception e) {
            return JsonResponse.errorResponseWithError("抱歉", "提交解决方案失败");
        }finally {
            lock.unlock();
        }
    }

    @ApiOperation("获取投诉解决方案列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页数", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "getComplaintSolutionList", method = RequestMethod.GET)
    public JsonResponse getComplaintSolutionList(@ApiIgnore Principal principal, Pageable pageable,
        @ApiParam("投诉报告ID") @RequestParam(value = "complaintReportId") Long complaintReportId) {
        if (principal == null) {
            return JsonResponse.errorResponseWithError("抱歉", "invalid_token");
        }
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        return complaintSolutionService.getComplaintSolutionList(complaintReportId, pageable);
    }


}
